using System;
using System.Data;
using System.Xml;
using vs.Core;

namespace Forum
{
    public partial class RSS : System.Web.UI.Page
    {
        private const int PAGESIZE = 30;

        private XmlElement CreateItem(XmlDocument doc, string link, string title, string desc, DateTime d)
        {
            XmlElement news = doc.CreateElement("item");
            string pubdate = d.DayOfWeek.ToString().Substring(0, 3) + ", " + d.ToString("dd MMM yyyy HH:mm:ss") + " GMT";

            CommonFunction.AddNode(doc, news, "link", link);
            CommonFunction.AddNode(doc, news, "title", title);
            CommonFunction.AddNode(doc, news, "description", desc);
            CommonFunction.AddNode(doc, news, "pubDate", pubdate);

            return news;
        }

        private XmlDocument GenDocForum(string forumID)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<rss version=\"2.0\"></rss>");
            XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDecl, root);

            XmlElement channel = doc.CreateElement("channel");
            root.AppendChild(channel);
            CommonFunction.AddNode(doc, channel, "title", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "link", "http://" + PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "description", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "copyright", "VSP - AWAS");
            CommonFunction.AddNode(doc, channel, "generator", "VSP Generator");

            string strSQL = "SELECT t.TopicID, t.TopicName, t.PostedDate, f.ForumName, t.Creator "
                + " FROM FORUM_Forum f INNER JOIN FORUM_Topic t ON f.ForumID = t.ForumID "
                + " WHERE (t.ForumID = @ForumID) ORDER BY t.PostedDate DESC";
            DataParameter p = new DataParameter("@ForumID", forumID);
            DataTable dt = DataFactory.LoadPage(strSQL, 0, PAGESIZE, p);

            if (dt.Rows.Count > 0)
            {
                ModSettingController db = new ModSettingController();
                string topicdesk = db.Load(Utils.ModuleID, "topicdesk");
                
                string titleTemplate = "@@TopicName [@@UserName] - " + dt.Rows[0]["ForumName"].ToString();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string link = "http://" + PortalUtils.HostName + UrlProvider.DesktopUrl(topicdesk, dt.Rows[i]["TopicID"].ToString());
                    string title = titleTemplate.Replace("@@UserName", dt.Rows[i]["Creator"].ToString());
                    title = title.Replace("@@TopicName", dt.Rows[i]["TopicName"].ToString()); 
                    string desc = "";
                    DateTime pubDate = Convert.ToDateTime(dt.Rows[i]["PostedDate"]);
                    XmlElement item = CreateItem(doc, link, title, desc, pubDate);
                    channel.AppendChild(item);
                }
            }

            return doc;

        }

        private XmlDocument GenDocTopic(int topicID)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<rss version=\"2.0\"></rss>");
            XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDecl, root);

            XmlElement channel = doc.CreateElement("channel");
            root.AppendChild(channel);
            CommonFunction.AddNode(doc, channel, "title", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "link", "http://" + PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "description", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "copyright", "VSP - AWAS");
            CommonFunction.AddNode(doc, channel, "generator", "VSP Generator");

            string strSQL = "SELECT r.ReplyID, r.Content, r.UserName, r.PostedDate, t.TopicName "
                + " FROM FORUM_Topic t INNER JOIN FORUM_Reply r ON t.TopicID = r.TopicID "
                + " WHERE (r.TopicID = @TopicID) ORDER BY r.PostedDate DESC";
            DataParameter p = new DataParameter("@TopicID", topicID);
            DataTable dt = DataFactory.LoadPage(strSQL, 0, PAGESIZE, p);

            if (dt.Rows.Count > 0)
            {
                ModSettingController db = new ModSettingController();
                string topicdesk = db.Load(Utils.ModuleID, "topicdesk");
                string link = "http://" + PortalUtils.HostName + UrlProvider.DesktopUrl(topicdesk, topicID.ToString());
                string titleTemplate = dt.Rows[0]["TopicName"].ToString() + " [@@UserName] - " + PortalUtils.HostName;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string title = titleTemplate.Replace("@@UserName", dt.Rows[i]["UserName"].ToString());
                    string desc = CommonFunction.ToHtmlContent(dt.Rows[i]["Content"].ToString());
                    DateTime pubDate = Convert.ToDateTime(dt.Rows[i]["PostedDate"]);
                    XmlElement item = CreateItem(doc, link, title, desc, pubDate);
                    channel.AppendChild(item);
                }
            }

            return doc;
        }

        private XmlDocument GenDocLatestReply()
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml("<rss version=\"2.0\"></rss>");
            XmlDeclaration xmlDecl = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDecl, root);

            XmlElement channel = doc.CreateElement("channel");
            root.AppendChild(channel);
            CommonFunction.AddNode(doc, channel, "title", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "link", "http://" + PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "description", PortalUtils.HostName);
            CommonFunction.AddNode(doc, channel, "copyright", "VSP - AWAS");
            CommonFunction.AddNode(doc, channel, "generator", "VSP Generator");

            string strSQL = "SELECT TopicID, TopicName, ModifyDate, Creator FROM FORUM_Topic ORDER BY ModifyDate DESC";
            DataTable dt = DataFactory.LoadPage(strSQL, 0, PAGESIZE);

            if (dt.Rows.Count > 0)
            {
                ModSettingController db = new ModSettingController();
                string topicdesk = db.Load(Utils.ModuleID, "topicdesk");

                string titleTemplate = "@@TopicName [@@Creator]";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string link = "http://" + PortalUtils.HostName + UrlProvider.DesktopUrl(topicdesk, dt.Rows[i]["TopicID"].ToString());
                    string title = titleTemplate.Replace("@@Creator", dt.Rows[i]["Creator"].ToString());
                    title = title.Replace("@@TopicName", dt.Rows[i]["TopicName"].ToString());
                    string desc = "";

                    DateTime pubDate = Convert.ToDateTime(dt.Rows[i]["ModifyDate"]);
                    XmlElement item = CreateItem(doc, link, title, desc, pubDate);
                    channel.AppendChild(item);
                }
            }

            return doc;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    XmlDocument doc = null;
                    if (Request.QueryString["tid"] != null)
                    {
                        int topicID = Convert.ToInt32(Request.QueryString["tID"]);
                        doc = GenDocTopic(topicID);
                    }
                    else if (Request.QueryString["fid"] != null)
                    {
                        string forumID = Request.QueryString["fID"];
                        doc = GenDocForum(forumID);
                    }
                    else
                        doc = GenDocLatestReply();

                    Response.ContentType = "text/xml";
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.Write(doc.OuterXml);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }
}
